//
//  SearchScenicVM.swift
//  LeTu
//
//  Created by g1 on 2024/1/14.
//

import Foundation

struct SearchSectionItem {
    var title = ""
    var iconName = ""
}

class SearchScenicVM {
    
    /// section 信息
    private(set) var sectionItems = [SearchSectionItem]()
    /// 城市信息
    var citys = [CityModel]()
    /// 历史搜索
    var historys: [String] {
        kHistoryKeywordsManager.getKeywords()
    }
    /// 热门搜索
    var hotKeywords = [CityModel]()
    /// 国家分类 0：获取中国的所有城市，1：获取国外的所有城市
    var countryType = 0
    
    init() {
        updateItems()
    }
    
    /// 更新section信息
    func updateItems() {
        
        sectionItems = [SearchSectionItem(title: "分类".localized), SearchSectionItem(title: "热门城市".localized)]
        
        guard historys.count > 0 else { return }
        sectionItems.insert(SearchSectionItem(title: "搜索历史".localized, iconName: "history_delete"), at: 1)
        
    }
    
    /// 获取热门搜索
    func getHotSearchKeys(completeHolder: @escaping NetworkRequestHandle) {
        
        apiProvider.request(api: .getHotCity) { [weak self] json, jsonArr, code in
            MYLog("获取热门搜索", json)
            var dialog = DialogMessage()

            guard let response = Mapper<ResponseModel>().map(JSON: json) else { return completeHolder(dialog) }
            dialog.code = response.code
            dialog.message = response.msg
            guard let strongSelf = self ,dialog.code == 200, let data = response.data as? [[String: Any]] else { return completeHolder(dialog) }
            strongSelf.hotKeywords = Mapper<CityModel>().mapArray(JSONArray: data)
            completeHolder(dialog)
        } failureHandler: {  error in
            completeHolder(DialogMessage())
        }

        
    }
    
    /// 搜索关键词联想
    func searchKeywordAssociate(keyword: String, completeHolder: @escaping NetworkRequestHandle) {
        
        apiProvider.request(api: .searchCity(params: ["cityName": keyword, "countryType": countryType])) { json, jsonArr, code in
            MYLog("搜索关键词联想", json)
            var dialog = DialogMessage()

            guard let response = Mapper<ResponseModel>().map(JSON: json) else { return completeHolder(dialog) }
            dialog.code = response.code
            dialog.message = response.msg
            guard dialog.code == 200, let data = response.data as? [String: Any], let citys = data["rows"] as? [[String: Any]] else { return completeHolder(dialog) }
            dialog.data = Mapper<CityModel>().mapArray(JSONArray: citys).map({ LanguageManager.shared.currentLanguage == .zh ? $0.name:$0.englishName })
            completeHolder(dialog)
        } failureHandler: {  error in
            completeHolder(DialogMessage())
        }
        
    }
    
    /// 获取国内城市列表
    func getChinaCitys(completeHolder: @escaping NetworkRequestHandle) {
        
        apiProvider.request(api: .getChinaCitys(params: [:])) { [weak self] json, jsonArr, code in
            MYLog("获取国内城市列表", json)
            var dialog = DialogMessage()

            guard let strongSelf = self, let response = Mapper<ResponseModel>().map(JSON: json) else { return completeHolder(dialog) }
            dialog.code = response.code
            dialog.message = response.msg
            guard dialog.code == 200, let data = response.data as? [[String: Any]] else { return completeHolder(dialog) }
            strongSelf.citys = Mapper<CityModel>().mapArray(JSONArray: data).filter { $0.cities.count > 0 }
            completeHolder(dialog)
        } failureHandler: {  error in
            completeHolder(DialogMessage())
        }
        
    }
    
    /// 获取海外城市列表
    func getForeignCitys(completeHolder: @escaping NetworkRequestHandle) {
        
        apiProvider.request(api: .getForeignCitys(params: [:])) { [weak self] json, jsonArr, code in
            MYLog("获取海外城市列表", json)
            var dialog = DialogMessage()

            guard let strongSelf = self, let response = Mapper<ResponseModel>().map(JSON: json) else { return completeHolder(dialog) }
            dialog.code = response.code
            dialog.message = response.msg
            guard dialog.code == 200, let data = response.data as? [[String: Any]] else { return completeHolder(dialog) }
            strongSelf.citys = Mapper<CityModel>().mapArray(JSONArray: data)
            completeHolder(dialog)
        } failureHandler: {  error in
            completeHolder(DialogMessage())
        }
        
    }
    
}
